package com.ssyt.ec.sys.service;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.sys.entity.MenuEntity;

/**
 * 菜单管理
 * 
 * @author huanglinfeng
 *
 */
public class SysMenuService {

	/**
	 * 修改
	 * 
	 * @param id
	 * @param menuName
	 * @param url
	 * @param state
	 * @return
	 */
	public int updateMenu(int id, String menuName, String url, int state) {
		return DBUtil.executeUpdate(
				"UPDATE sys_menu SET text=?,url=?,state=? WHERE id=?",
				menuName, url, state, id);
	}

	/**
	 * 修改状态
	 * 
	 * @param id
	 * @param value
	 * @return
	 */
	public int updateState(int id, int value) {
		return DBUtil.executeUpdate("UPDATE sys_menu SET state=? WHERE id=?",
				value, id);
	}

	/**
	 * 显示启用的菜单
	 * 
	 * @return
	 */
	public PageModel<MenuEntity> listMenuop(int page, int rows) {
		return DBUtil.executeQueryByPage(MenuEntity.class,
				"SELECT * FROM sys_menu WHERE state=1 ORDER BY id LIMIT ?, ?",
				(page - 1) * rows, rows);
	}

	/**
	 * 显示禁用的菜单
	 * 
	 * @return
	 */
	public PageModel<MenuEntity> listMenuclo(int page, int rows) {
		return DBUtil.executeQueryByPage(MenuEntity.class,
				"SELECT * FROM sys_menu WHERE state=2 ORDER BY id LIMIT ?, ?",
				(page - 1) * rows, rows);
	}

	/**
	 * 菜单的新增
	 * 
	 * @param pid
	 * @param text
	 * @param url
	 * @param state
	 * @return
	 */
	public int addMenu(int pid, String text, String url, int state) {
		Date date = new Date();
		String icon = "icon_man";
		return DBUtil.executeUpdate(
				"INSERT INTO sys_menu VALUES (NULL,?,?,?,NULL,?,?,?)", pid,
				text, url, date, state, icon);
	}

	/**
	 * 启用
	 * 
	 * @param ids
	 * @param userId
	 * @return
	 */
	public int open(String[] ids, int userId) {
		String sql = "UPDATE sys_menu SET state=1,id=?,operate_time=NOW() WHERE id IN ("
				+ StringUtils.join(ids) + ")";
		return DBUtil.executeUpdate(sql, userId);
	}
}
